﻿Imports System.Activities
Imports System.ComponentModel
Imports System.Activities.Presentation.PropertyEditing
Imports System.Collections.ObjectModel
Imports System.Activities.Presentation.Metadata
Imports System.Drawing

Public Class FolderDeleteActivity
    Inherits AsyncCodeActivity
    Implements IRegisterMetadata

    <Browsable(True)>
    Public Property TargetFolder As InArgument(Of String)

    <Browsable(True)>
    Public Property Result As OutArgument(Of Boolean)

    Private Delegate Function asyncDeleteFolderDelegate(ByVal target As String) As Boolean

    Public Sub New()
        Me.DisplayName = My.Resources.FolderDeleteDisplayName
    End Sub

    Protected Overrides Function BeginExecute(ByVal context As System.Activities.AsyncCodeActivityContext, ByVal callback As System.AsyncCallback, ByVal state As Object) As System.IAsyncResult
        Dim target = context.GetValue(Me.TargetFolder)
        Dim asyncExecute = New asyncDeleteFolderDelegate(AddressOf DeleteFolder)
        context.UserState = asyncExecute

        Return asyncExecute.BeginInvoke(target, callback, state)
    End Function

    Protected Overrides Sub EndExecute(ByVal context As System.Activities.AsyncCodeActivityContext, ByVal result As System.IAsyncResult)
        Dim asyncExecute = TryCast(context.UserState, asyncDeleteFolderDelegate)
        Dim getResult = asyncExecute.EndInvoke(result)

        context.SetValue(Me.Result, getResult)
    End Sub

    Private Function DeleteFolder(ByVal target As String) As Boolean

        Dim result As Boolean = False
        Try
            My.Computer.FileSystem.DeleteDirectory(target, FileIO.DeleteDirectoryOption.DeleteAllContents)
            result = True
        Catch ex As Exception

        End Try
        Return result
    End Function

    Public Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim build As New AttributeTableBuilder
        'FolderDelete Activity
        build.AddCustomAttributes(GetType(FolderDeleteActivity),
                                              New ToolboxBitmapAttribute(GetType(FolderDeleteActivity), "FolderDelete.png"))
        build.AddCustomAttributes(GetType(FolderDeleteActivity), "TargetFolder",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(FolderpathPropertyEditor), GetType(DialogPropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.FolderDeleteCategory),
                                              New DisplayNameAttribute(My.Resources.FolderDeleteTargetFolderDisplay),
                                              New DescriptionAttribute(My.Resources.FolderDeleteTargetFolderDescription))
        build.AddCustomAttributes(GetType(FolderDeleteActivity), "Result",
                                              New CategoryAttribute(My.Resources.FolderDeleteCategory),
                                              New DisplayNameAttribute(My.Resources.FolderDeleteResultDisplay),
                                              New DescriptionAttribute(My.Resources.FolderDeleteResultDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)

    End Sub
End Class
